package com.ethink.warehouse.facade.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.warehouse.api.MatePurchaseCodeFacade;
import com.ethink.warehouse.api.info.MatePurchaseCodeInfo;
import com.ethink.warehouse.api.request.MatePurchaseCodeQueryRequest;
import com.ethink.warehouse.api.request.MatePurchaseCodeUpdateRequest;
import com.ethink.warehouse.api.response.MatePurchaseCodeGetDetailResponse;
import com.ethink.warehouse.api.response.MatePurchaseCodeQueryResponse;
import com.ethink.warehouse.common.mapper.MatePurchaseCodeMapper;
import com.ethink.warehouse.service.MatePurchaseCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 库存采购批次表 Facade接口实现
 *
 * @author default
 * @date 2022-12-16
 */
@RestController
@Slf4j
@RequestMapping("warehouse-service/facade/v1/matePurchaseCode")
public class MatePurchaseCodeFacadeImpl implements MatePurchaseCodeFacade {

    @Autowired
    private MatePurchaseCodeService matePurchaseCodeService;

    @Autowired
    private MatePurchaseCodeMapper matePurchaseCodeMapper;


    @Override
    public MatePurchaseCodeInfo getMatePurchaseCodeByProduceCode(String mateSku, String produceCode) {
        return matePurchaseCodeMapper.getMatePurchaseCodeByProduceCode(mateSku, produceCode);
    }

    @Override
    public List<MatePurchaseCodeInfo> getMatePurchaseCodeListByProduceCode(String mateSku, String excludeProduceCode) {
        List<MatePurchaseCodeInfo> list = matePurchaseCodeMapper.getMatePurchaseCodeListByProduceCode(mateSku, excludeProduceCode);
        // 按生产批次去重
        List<MatePurchaseCodeInfo> newList = new ArrayList<>();
        Set<String> produceCodeSet = new HashSet<>();
        for (MatePurchaseCodeInfo matePurchaseCodeInfo : list) {
            if (!produceCodeSet.contains(matePurchaseCodeInfo.getProduceCode())) {
                newList.add(matePurchaseCodeInfo);
                produceCodeSet.add(matePurchaseCodeInfo.getProduceCode());
            }
        }
        return newList;
    }

    @Override
    public Page<MatePurchaseCodeQueryResponse> queryMatePurchaseCode(MatePurchaseCodeQueryRequest request) {
        return matePurchaseCodeService.queryMatePurchaseCode(request);
    }

    @Override
    public MatePurchaseCodeGetDetailResponse getMatePurchaseCodeById(Long matePurchaseCodeId) {
        return matePurchaseCodeService.getMatePurchaseCodeById(matePurchaseCodeId);
    }

    @Override
    public void updateMatePurchaseCode(MatePurchaseCodeUpdateRequest request) {
        matePurchaseCodeService.updateMatePurchaseCode(request);
    }

}